#!/usr/bin/python
# vi: et sw=2
#

import pygtk
import gtk

from AppletModel import *

# This is the list of sinks
class AppletView(gtk.Window):
  # FIXME define signals

  def __init__(self, settings, model = None):
    gtk.Window.__init__(self, gtk.WINDOW_POPUP);

    self.model = model or AppletModel()
    self.box = None
    self.settings = settings

    self.model.connect("data-changed", self.update_view)

    # This make window grow and shrink
    self.set_property("resizable", False)

    print "AppletView is now initialized with model:", self.model
    pass

    #
    # __init__ end
    #

  def update_settings(self, settings):
    self.settings = settings
    self.update_view()  

    #
    # update_settings end
    #

  def update_view(self, model = None):
    box = self.settings["box_function"]()

    for i in range(0, self.model.sink_count()):
      box.add(self.build_sink_from_builder(i))

    box.add(gtk.VSeparator())

    for i in range(0, self.model.sink_input_count()):
      box.add(self.build_sink_input_from_builder(i))

    if self.box != None:
      self.remove(self.box)

    self.box = box
    self.add(self.box)

    if self.get_property("visible") == True:
      self.show_all()

    pass

    #
    # update_view end
    #

  def build_sink_from_builder(self, index):
    wTree = self.settings["layout_function"]()

    return self.build_widget(wTree,
                             index,
                             "sink",
                             self.model.sink_get_volume,
                             self.model.sink_get_name,
                             self.model.sink_get_desc,
                             self.model.sink_get_mute)

    #
    # build_sink_from_builder end
    #

  def build_sink_input_from_builder(self, index):
    wTree = self.settings["layout_function"]()

    return self.build_widget(wTree,
                             index,
                             "sink_input",
                             self.model.sink_input_get_volume,
                             self.model.sink_input_get_name,
                             self.model.sink_input_get_desc,
                             self.model.sink_input_get_mute)

    #
    # build_sink_input_from_builder end
    #

  def build_widget(self, wTree, index, sink_type, get_volume,
                         get_name, get_desc, get_mute):
    sink = wTree.get_object("sink")

    if sink == None:
      return gtk.Label("Check builderfile")

    sink.sink_index = index
    sink.sink_type = sink_type

    volume_adjust = wTree.get_object("volume_adjust")
    mute = wTree.get_object("mute")

    if mute != None and isinstance(mute, gtk.Button):
      if get_mute(index):
        mute.set_label("M");
      else:
        mute.set_label(str(get_volume(index)))

      mute.connect("button-press-event", self.on_mute_button_press_event, sink)

    if volume_adjust != None:
      volume_adjust.set_value(get_volume(index))
      volume_adjust.connect("value-changed", self.volume_changed_event, sink)

    return sink

    #
    # build_widget end
    #

  # Events
  #

  def volume_changed_event(self, adjust, sink_widget):
    volume = int(adjust.get_value())
    type   = sink_widget.sink_type
    index  = sink_widget.sink_index

    if type == "sink":
      self.model.sink_set_volume(index, volume)
    elif type == "sink_input":
      self.model.sink_input_set_volume(index, volume)

    pass

    #
    # volume_changed_event end
    #

  def on_mute_button_press_event(self, button, event, sink_widget):
    type  = sink_widget.sink_type
    index = sink_widget.sink_index

    mute  = None

    if type == "sink":
      mute = self.model.sink_get_mute(index)
      self.model.sink_set_mute(index, not mute)
    elif type == "sink_input":
      mute = self.model.sink_input_get_mute(index)
      self.model.sink_input_set_mute(index, not mute)

  pass
